package com.example.ggpicturegg.ftpUD;

import android.graphics.Bitmap;

import com.example.ggpicturegg.tool.BlobAndBytes;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTP;

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

public class UpAndDown {
    /**
     * ftp上传
     * @param fileNamePath  本地文件绝对路径
     * @param fileName  本地文件名
     * @param id 序列号
     * @return
     */
    public static String ftpUpload( String fileNamePath,String fileName,int id){
        FTPClient ftpClient = new FTPClient();
        FtpInfo ftpInfo = new FtpInfo();
        FileInputStream fis = null;
        String returnMessage = "0";
        try {
            ftpClient.connect(ftpInfo.getUrl(), Integer.parseInt(ftpInfo.getPort()));
            boolean loginResult = ftpClient.login(ftpInfo.getUsername(), ftpInfo.getPassword());
            int returnCode = ftpClient.getReplyCode();
            if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功
                ftpClient.setBufferSize(1024);
                ftpClient.setControlEncoding("UTF-8");
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                ftpClient.enterLocalPassiveMode();
                fis = new FileInputStream(fileNamePath);
                fileNamePath = "No."+id+"-"+fileName;
                String fileNamePathUTF_8=new String(fileNamePath.getBytes("UTF-8"), "ISO-8859-1");
                ftpClient.storeFile(fileNamePathUTF_8, fis);

                System.out.println("----------上传成功--------------");
                returnMessage = "1";   //上传成功

            } else {// 如果登录失败
                System.out.println("----------上传失败--------------");
                returnMessage = "0";
            }

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("----------FTP客户端出错！--------------");
//		     throw new RuntimeException("FTP客户端出错！", e);
            returnMessage = "-1";
        } finally {
            //IOUtils.closeQuietly(fis);
            try {
                ftpClient.disconnect();
                System.out.println("----------FTP客户端关闭成功！--------------");
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("----------FTP客户端关闭真的出错了！--------------");
                throw new RuntimeException("关闭FTP连接发生异常！", e);
            }
        }
        return returnMessage;
    }

    /**
     * ftp下载
     * @param filePath 本地存放文件的路径
     * @param FTP_file 要下载
     *
     *
     *                 的文件名
     * @param SD_file 本地文件名
     */
    public static String ftpDown( String filePath,String FTP_file,String SD_file) throws UnsupportedEncodingException {
        BufferedOutputStream buffOut = null;
        FtpInfo ftpInfo = new FtpInfo();
        FTPClient ftpClient = new FTPClient();
        String f =new String(FTP_file.getBytes("UTF-8"), "ISO-8859-1");
        String returnMessage = "0";
        try {
            ftpClient.connect(ftpInfo.getUrl(), Integer.parseInt(ftpInfo.getPort()));
            boolean loginResult = ftpClient.login(ftpInfo.getUsername(), ftpInfo.getPassword());
            int returnCode = ftpClient.getReplyCode();
            if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功
                ftpClient.setBufferSize(1024);
                ftpClient.setControlEncoding("UTF-8");
                ftpClient.enterLocalPassiveMode();
                String fileNamePath = filePath+SD_file;
                String fileNamePathUTF_8=new String(fileNamePath.getBytes("UTF-8"), "ISO-8859-1");
                buffOut = new BufferedOutputStream(new FileOutputStream(fileNamePathUTF_8),8*1024);
                System.out.println(filePath+SD_file);

                boolean flg = ftpClient.retrieveFile(f, buffOut);
                buffOut.flush();
                buffOut.close();

                ftpClient.logout();
                ftpClient.disconnect();

               if(flg!=false){
                   System.out.println("----------下载成功！--------------");
               }else{
                   System.out.println("----------下载失败！--------------");
               }
                returnMessage = "1";   //下载成功

            } else {// 如果登录失败
                returnMessage = "0";
            }

        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错！", e);
        } finally {
            //IOUtils.closeQuietly(fis);
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭FTP连接发生异常！", e);
            }
        }
        return returnMessage;
    }

    public static InputStream ftpLook(String FTP_file,int id){
        FTP_file = "No."+id+"-"+FTP_file;
       // System.out.println("FTP_file is ----------------  "+FTP_file);
        InputStream inputStream = null;
        Bitmap bitmap;
        FtpInfo ftpInfo = new FtpInfo();
        FTPClient ftpClient = new FTPClient();
        String returnMessage = "0";
        try {
            ftpClient.connect(ftpInfo.getUrl(), Integer.parseInt(ftpInfo.getPort()));
            boolean loginResult = ftpClient.login(ftpInfo.getUsername(), ftpInfo.getPassword());
            int returnCode = ftpClient.getReplyCode();
            if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功
                ftpClient.setBufferSize(1024);
                ftpClient.setControlEncoding("UTF-8");
                ftpClient.enterLocalPassiveMode();
                inputStream = ftpClient.retrieveFileStream(new String(FTP_file.getBytes("UTF-8"), "ISO-8859-1"));

                ftpClient.logout();
                ftpClient.disconnect();

                if(inputStream!=null){
                    System.out.println("----------查询成功！--------------");
                }else{
                    System.out.println("----------查询失败！--------------");
                }


            } else {// 如果登录失败
                System.out.println("----------登录失败！--------------");
                return null;
            }

        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错！", e);
        } finally {
            //IOUtils.closeQuietly(fis);
            try {
                ftpClient.disconnect();
                System.out.println("----------关闭  ftpClient  连接--------------");
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭FTP连接发生异常！", e);
            }
        }
        return inputStream;
    }

}
